from wormbane.ui.selection_dialog import selection_dialog
from wormbane.util.format import add_indefinite_article

def command_wear(game, ui):
    wearable = [item for item in game.hero.inv
                if item.is_armor and item.slot not in game.hero.eq]
    if not wearable:
        game.write(game.hero, 'has nothing to wear')
        return

    if len(wearable) == 1:
        selection = set(wearable)
    else:
        selection = selection_dialog(ui.stdscr, list(reversed(wearable)),
                                     prompt = 'Wear')
        if not selection:
            game.write('Never mind.')
            return

    for item in selection:
        if item.slot in game.hero.eq:
            game.write(game.hero, 'cannot wear', item)
        else:
            if item.wielded:
                game.hero.unwield(item)
            item = item.split()
            game.hero.wear(item)
            item.enchantment_id = True
            game.write(game.hero, 'wears', item)
            game.hero_moved = True
